package bmark

import grails.converters.*

class BookmarkController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def bookmarkService

    def index = {
        redirect(uri:"/timeline")
    }

    def timeline = {
    }

    def list = {
        //params.max = Math.min(params.max ? params.int('max') : 10, 100)
        render Bookmark.listOrderByDateCreated(params, order:'asc') as JSON
    }   


    def save = {
        def bookmarkInstance = new Bookmark(params)
        if (bookmarkService.saveBookmark(bookmarkInstance)) {
            response.status = 201
            render bookmarkInstance as JSON
        } else {
            response.status = 400
            render 'error'
        }
    }


    def delete = {
        def bookmarkInstance = Bookmark.get(params.id)
        if (bookmarkInstance) {
            try {
                bookmarkInstance.delete(flush: true)
                render 'Ok'
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'bookmark.label', default: 'Bookmark'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            response.status = 404
            render 'Not found'
        }
    }
}
